package com.yunji.cabinet.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.base.domain.BelongDO;
import com.yunji.cabinet.bo.CabinetCellListQueryBO;
import com.yunji.cabinet.bo.CabinetCellSelectQueryBO;
import com.yunji.cabinet.bo.CabinetCellUpdateBO;
import com.yunji.cabinet.bo.FillCargoBO;
import com.yunji.cabinet.dao.ICabinetCellDao;
import com.yunji.cabinet.domain.ContainerDO;
import com.yunji.cabinet.domain.ContainerCell;
import com.yunji.cabinet.mapper.CabinetCellMapper;
import com.yunji.cabinet.vo.CellProductCountVO;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Repository
public class CabinetCellDaoImpl extends BaseDaoImpl<CabinetCellMapper, ContainerCell> implements ICabinetCellDao {

    @Override
    public boolean deleteByContainerIds(Long... ids) {
        return remove(new LambdaQueryWrapper<ContainerCell>()
                .in(ContainerCell::getContainerId, Arrays.asList(ids)));
    }

    @Override
    public List<ContainerCell> searchCountCell(CabinetCellListQueryBO bo) {
        MPJLambdaWrapper<ContainerCell> wrapper = new MPJLambdaWrapper<>();
        return selectJoinList(ContainerCell.class, wrapper.select(ContainerCell::getCellId)
                .select(ContainerCell::getContainerId)
                .select(ContainerCell::getProductId)
                .select(ContainerCell::getOrdinal)
                .select(ContainerCell::getStatus)
                .leftJoin(ContainerDO.class, ContainerDO::getContainerId, ContainerCell::getContainerId)
                .leftJoin(BelongDO.class, BelongDO::getBelongId, ContainerDO::getBelongId)
                .eq(BeanUtils.isNotNull(bo.getStatus()), ContainerCell::getStatus, bo.getStatus())
                .eq(BeanUtils.isNotNull(bo.getManagerId()), BelongDO::getSiteId, bo.getManagerId())
                .eq(BeanUtils.isNotNull(bo.getProductId()), ContainerCell::getProductId, bo.getProductId()));
    }

    @Override
    public boolean update(CabinetCellUpdateBO bo) {
        return update(new LambdaUpdateWrapper<ContainerCell>()
                .set(BeanUtils.isNotNull(bo.getProductId()), ContainerCell::getProductId, bo.getProductId())
              //  .set(BeanUtils.isNotNull(bo.getUnlock()), CabinetCell::getUnlock, bo.getUnlock())
                .set(BeanUtils.isNotNull(bo.getStatus()), ContainerCell::getStatus, bo.getStatus())
                .set(BeanUtils.isNotNull(bo.getStatusUpdateTime()), ContainerCell::getStatusUpdateTime, bo.getStatusUpdateTime())
                .set(BeanUtils.isNotNull(bo.getLackStatus()), ContainerCell::getLackStatus,bo.getLackStatus())
                .set(BeanUtils.isNotNull(bo.getLackTime()), ContainerCell::getLackTime,bo.getLackTime())
                .set(BeanUtils.isNotNull(bo.getSendStatus()), ContainerCell::getSendStatus, bo.getSendStatus())
//                .set(BeanUtils.isNotNull(bo.getIdentityHeight()),CabinetCell::getIdentityHeight, bo.getIdentityHeight())
//                .set(BeanUtils.isNotNull(bo.getInfraredStatus()),CabinetCell::getInfraredStatus, bo.getInfraredStatus())
                .set(BeanUtils.isNotNull(bo.getProductQuantity()), ContainerCell::getProductQuantity, bo.getProductQuantity())
                .set(BeanUtils.isNotNull(bo.getCeilingQuantity()), ContainerCell::getCeilingQuantity, bo.getCeilingQuantity())
                .eq(BeanUtils.isNotNull(bo.getCellId()), ContainerCell::getCellId, bo.getCellId())
                .eq(BeanUtils.isNotNull(bo.getOrdinal()), ContainerCell::getOrdinal, bo.getOrdinal())
                .eq(BeanUtils.isNotNull(bo.getContainerId()), ContainerCell::getContainerId, bo.getContainerId()));
    }

    @Override
    public int getRemainProduct(Long containerId) {
        return baseMapper.getRemainProduct(containerId);
    }

    @Override
    public ContainerCell select(CabinetCellSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<ContainerCell>()
                .eq(BeanUtils.isNotNull(bo.getOrdinal()), ContainerCell::getOrdinal, bo.getOrdinal())
                .eq(BeanUtils.isNotNull(bo.getCabinetId()), ContainerCell::getContainerId, bo.getCabinetId()));
    }

    @Override
    public boolean addProduct(Long cabinetId, Integer... nums) {
        return update(new LambdaUpdateWrapper<ContainerCell>()
                .set(ContainerCell::getStatus, LogicCode.YES)
                .set(ContainerCell::getSendStatus, LogicCode.YES)
                .set(ContainerCell::getStatusUpdateTime, TimeUtils.getNowDate())
                .eq(ContainerCell::getContainerId, cabinetId)
                .in(ContainerCell::getOrdinal, Arrays.asList(nums)));
    }

    @Override
    public List<CellProductCountVO> countCellProduct(Long belongId) {
        return baseMapper.countCellProduct(belongId);
    }

    @Override
    public List<FillCargoBO> searchFillCargoCells(LocalDateTime localDateTime) {
        MPJLambdaWrapper<ContainerCell> wrapper = new MPJLambdaWrapper<>();
        return selectJoinList(FillCargoBO.class, wrapper.selectAll(ContainerCell.class)
                .leftJoin(ContainerDO.class, ContainerDO::getContainerId, ContainerCell::getContainerId)
                .eq(ContainerCell::getStatus, LogicCode.NO)
                .eq(ContainerCell::getSendStatus, LogicCode.NO)
                .le(ContainerCell::getStatusUpdateTime, localDateTime));
    }

    @Override
    public List<ContainerCell> searchByContainerId(Long containerId) {
        return list(new LambdaQueryWrapper<ContainerCell>()
                .eq(ContainerCell::getContainerId, containerId));
    }

    @Override
    public List<ContainerCell> search(CabinetCellListQueryBO bo) {
        return list(new LambdaQueryWrapper<ContainerCell>()
                .eq(BeanUtils.isNotNull(bo.getContainerId()), ContainerCell::getContainerId, bo.getContainerId())
                .in(BeanUtils.isNotNull(bo.getOrdinals()), ContainerCell::getOrdinal, ConvertDataUtil.array2List(bo.getOrdinals())));
    }

    @Override
    public List<String> searchProductIdsByContainerId(List<Long> containerIds) {
        return listObjs(new LambdaQueryWrapper<ContainerCell>()
                .select(ContainerCell::getProductId)
                .in(ContainerCell::getContainerId,containerIds),Object::toString
        );
    }

    @Override
    public List<ContainerCell> listByContainerId(Long containerId) {
        return lambdaQuery().eq(ContainerCell::getContainerId, containerId).list();
    }
}
